package CCRMClient.GUI;
import java.awt.Color;
import java.awt.Component;

import javax.swing.border.LineBorder;
import javax.swing.plaf.synth.Region;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientCustomerManagement;
import CCRMServer.DBConnect;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
/**
 * This class implements add region screen panel
 * @author Ola
 *
 */
public class AddRegionScreen extends JPanel {
	
	private JLabel lblAddRegion;
	private JLabel lblRegionID;
	private JLabel lblRegionName;
	private JLabel lblregionDescription;
	private JTextField textRegionID;
	private JTextField textRegionName;
	private JTextField textRegionDescription;
	private JButton btnAddRegion;
	private JButton btnCancel;
	
	/**
	 * Create add region panel
	 */
	public AddRegionScreen(){
		initialize();
	}
	
	
	private void initialize(){
		setSize (565,549);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblAddRegion());
		add( getLlblRegionID());
		add(getLblRegionName());
		add(getLblegionDescription());
		add( getTextRegionID());
		add(getTextRegionName());
		add(getTextRegionDescription());
		add(getBtnAddRegion());
		add(getBtnCancel());
	}
		JLabel getLblAddRegion(){
			if(lblAddRegion==null){
				lblAddRegion=new JLabel("Add Region");
				lblAddRegion.setHorizontalAlignment(SwingConstants.CENTER);
				lblAddRegion.setForeground(Color.RED);
				lblAddRegion.setFont(new Font("David", Font.BOLD, 26));
				lblAddRegion.setBackground(Color.WHITE);
				lblAddRegion.setBounds(0, 13, 565, 39);
			}
			return lblAddRegion;
	
	}
		JLabel getLlblRegionID(){
			
			if(lblRegionID==null){
				lblRegionID=new JLabel("Region ID");
				lblRegionID.setFont(new Font("David", Font.BOLD, 20));
				lblRegionID.setBounds(30, 80, 164, 29);
			}
			return lblRegionID;
			
		}
		
		JLabel getLblRegionName(){
			if(lblRegionName==null){
				lblRegionName= new JLabel("Region Name");
				lblRegionName.setFont(new Font("David", Font.BOLD, 20));
				lblRegionName.setBounds(30, 142, 164, 29);
			}
				
			
			return lblRegionName;
			
		}
		
		JLabel getLblegionDescription(){
			
			
			if(lblregionDescription == null){
				lblregionDescription=new JLabel("Description");
				lblregionDescription.setFont(new Font("David", Font.BOLD, 20));
				lblregionDescription.setBounds(30, 207, 164, 29);
			}
			return lblregionDescription;
			
		}
	
		JTextField getTextRegionID() {
			if (textRegionID == null) {
				textRegionID = new JTextField();
				textRegionID.setColumns(10);
				textRegionID.setBackground(SystemColor.menu);
				textRegionID.setBounds(237, 85, 251, 20);
			}
			
			return textRegionID ;
		}
		JTextField getTextRegionName() {
			if (textRegionName == null) {
				textRegionName = new JTextField();
				textRegionName.setColumns(10);
				textRegionName.setBackground(SystemColor.menu);
				textRegionName.setBounds(237, 147, 251, 20);
			}
			
			
			return textRegionName;
		}
		JTextField getTextRegionDescription() {
			if (textRegionDescription == null) {
				textRegionDescription = new JTextField();
				textRegionDescription.setColumns(10);
				textRegionDescription.setBackground(SystemColor.menu);
				textRegionDescription.setBounds(237, 212, 251, 20);
			}
			return textRegionDescription;
		}
		
		JButton getBtnCancel(){
			if (btnCancel == null) {
				btnCancel = new JButton("Cancel");
				btnCancel.addActionListener(new BtnCancelActionListener());
				btnCancel.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnCancel.setBounds(360, 460, 149, 23);
		}
			return btnCancel;
			
		}
		
		JButton getBtnAddRegion(){
			if (btnAddRegion == null) {
				btnAddRegion = new JButton("Add Region");
				btnAddRegion.addActionListener(new BtnAddRegionActionListener());
				btnAddRegion.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnAddRegion.setBounds(106, 460, 149, 23);
		}
			return btnAddRegion;
			
		}
		 /**
		  * This inner class implements event listener for add cancel button
		  * @author Ola
		  *
		  */
		private class BtnCancelActionListener implements ActionListener {
			public void actionPerformed(ActionEvent arg0) {
				ClientStart.panelSwicher(new MarketingManagementScreen());
			}
	  }
		 /**
		  * This inner class implements event listener for add region button
		  * @author Ola
		  *
		  */
		private class BtnAddRegionActionListener implements ActionListener{
			public void actionPerformed(ActionEvent arg0) {
				try{ 
					int regionID=Integer.parseInt(textRegionID.getText());
					String regionName=textRegionName.getText();
					String regionDescription=textRegionDescription.getText();
					if(textRegionID.getText().isEmpty()||regionName.isEmpty())throw new NullPointerException();
					try{
						Regions region=new Regions (regionID,regionName,regionDescription);
						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_REGION,region);
					
					       } catch (IOException e1) {
								JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
								ClientStart.quitApp();
					}
	
					
				}
				catch (NullPointerException e) {
					JOptionPane.showMessageDialog(null, "All fields are mandatory", "Add customer problem", JOptionPane.INFORMATION_MESSAGE);
			}
		}
}
}
